/**
 * losenote.com Inc.
 * Copyright (c) 2013-2013 All Rights Reserved.
 */
package com.losenote.web.ct;

import java.sql.Date;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.losenote.base.util.DateUtil;
import com.losenote.enums.DataStateEnum;
import com.losenote.enums.QuestionTypeEnum;
import com.losenote.model.OperateContext;
import com.losenote.orm.ContactDetailDO;
import com.losenote.orm.FindNoteDO;
import com.losenote.orm.QuestionDO;
import com.losenote.service.req.FindNodePubReq;
import com.losenote.web.form.FindNoteForm;

/**
 * 转换form
 * 
 * @author danjingyu
 * @version $Id: FindNoteFormConvertor.java, v 0.1 2013-10-26 下午11:05:54 danjingyu Exp $
 */
public class FindNoteFormCt {

    /**
     * 将form对象转换为领域对象
     * @param findNoteform
     * @return
     */
    public static FindNodePubReq fromForm(FindNoteForm form, OperateContext context) {

        //联系信息
        FindNoteDO findNoteDO = new FindNoteDO();
        findNoteDO.setCategoryTypeId(form.getCategoryId());
        findNoteDO.setContent(form.getNoteContent());
        findNoteDO.setFindAddress(form.getAddressDetail());
        findNoteDO.setCreator(String.valueOf(context.getUserInfo().getId()));
        findNoteDO.setLastModifier(String.valueOf(context.getUserInfo().getId()));
        try {
            findNoteDO.setFindDate(new Date(DateUtil.parseDateNoTime(form.getFindDate(),
                DateUtil.webFormat).getTime()));
        } catch (ParseException e) {
        }

        findNoteDO.setPasswd(form.getPasswd());
        findNoteDO.setState(DataStateEnum.NORMAL.getValue());
        findNoteDO.setTitle(form.getTitle());

        //问题信息
        List<QuestionDO> questions = new ArrayList<QuestionDO>();
        if (!StringUtils.isBlank(form.getQuestionOne())) {
            QuestionDO questionOne = new QuestionDO();
            questionOne.setAnswer(form.getQuestionOne());
            questionOne.setQuestion(form.getAnswerOne());
            questionOne.setSequence(1);
            questionOne.setRelType(QuestionTypeEnum.FIND_NOTE.getValue());
            questions.add(questionOne);
        }
        if (!StringUtils.isBlank(form.getQuestionTwo())) {
            QuestionDO questionTwo = new QuestionDO();
            questionTwo.setAnswer(form.getQuestionTwo());
            questionTwo.setQuestion(form.getAnswerTwo());
            questionTwo.setSequence(2);
            questionTwo.setRelType(QuestionTypeEnum.FIND_NOTE.getValue());
            questions.add(questionTwo);
        }

        //联系信息
        ContactDetailDO contactDetailDO = new ContactDetailDO();
        contactDetailDO.setName(form.getContactName());
        contactDetailDO.setAddress(form.getAddressDetail());
        contactDetailDO.setMail(form.getEmail());
        contactDetailDO.setTelPhone(form.getMobile());

        FindNodePubReq req = new FindNodePubReq();
        req.setContactDo(contactDetailDO);
        req.setFindNoteDO(findNoteDO);
        req.setQuestionDos(questions);
        return req;
    }
}
